import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import logic.ConfigInfo;
import logic.Monitor;
import logic.NettyClient;

// import org.apache.log4j.Logger;

/**
 * 服务端启动窗体类
 * @author  骆焕
 * @since   2018.04.23
 * @version 1.0
 */
public class LaunchWin extends JFrame {

	// 序列版本UID
	private static final long serialVersionUID = 1L;
	
	// 按钮
	JButton startButton, stopButton, confirmButton; 
	// 文本框
	JTextArea sendArea;
	// 密码框
	JPasswordField passwordField;
	// 面板
	JPanel mainPanel, pwdPanel, btnPanel;
	JScrollPane contentPanel;
	// 配置信息
	private final ConfigInfo configInfo = ConfigInfo.getInstance();
	 	
	// 密码
	final String password = "123";
	
	// 日志对象
	// private final static Logger logger = Logger.getLogger(ServerWin.class);
	
	/**
	 * 构造函数
	 */
	public LaunchWin() {
		mainPanel = new JPanel();
		btnPanel = new JPanel();
		pwdPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
	    
	    // 添加按钮
	    startButton = new JButton("启动");
	    stopButton = new JButton("停止");
	    stopButton.setEnabled(false);
	    btnPanel.add(startButton);
	    btnPanel.add(stopButton);
	    
	    // 添加文本框
	    sendArea = new JTextArea("");
	    sendArea.setSize(100, 100);
	    sendArea.setEditable(false);
	    contentPanel = new JScrollPane(sendArea);
	    contentPanel.setBorder(new TitledBorder("接收内容"));
	    
	    // 添加密码框
	    JLabel label = new JLabel("密码: ");
	    pwdPanel.add(label, BorderLayout.WEST);
	    passwordField = new JPasswordField(10);
	    pwdPanel.add(passwordField, BorderLayout.CENTER);
	    confirmButton = new JButton("确定");
	    pwdPanel.add(confirmButton, BorderLayout.EAST);
	    pwdPanel.setSize(630, 40);
	    
	    // 设置面板的隐藏和显示
	    passwordField.setVisible(true);
	    btnPanel.setVisible(false);
	    contentPanel.setVisible(false);
	    
	    // 添加面板
	    mainPanel.add(pwdPanel, BorderLayout.CENTER);
		mainPanel.add(btnPanel, BorderLayout.NORTH);
	    mainPanel.add(contentPanel, BorderLayout.CENTER);
	    
	    // 设置内容面板
	    btnPanel.setSize(630, 32);
	    setContentPane(mainPanel);
	    
	    // 添加事件
	    this.AddEvent();
	}
	/**
	 * 添加事件处理
	 */
	void AddEvent() {
		
		/**
		 * 确定按钮事件
		 */
		confirmButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String inputPwd = String.valueOf(passwordField.getPassword());
				// 密码输入正确
				if (password.equals(inputPwd)) {
					// 主面板移除密码 	
					pwdPanel.setVisible(false);
					btnPanel.setVisible(true);
				    contentPanel.setVisible(true);
				}
			}
		});
		
		/**
	     * 启动按钮事件
	     */
	    startButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 启动按钮置灰
				startButton.setEnabled(false);
				// 停止按钮可用
				stopButton.setEnabled(true);
				// 客户端线程
				NettyClient nettyClient = new NettyClient(configInfo.serverIP, configInfo.serverPort, sendArea);

				// 监控线程
				Monitor monitor = new Monitor(nettyClient);
				Thread thread = new Thread(monitor);
				thread.setName("监控线程");
				
				// 将监控线程转为精灵线程
				thread.setDaemon(true);
				thread.start();

				nettyClient.start();
			}
		});
	    
	    /**
	     * 停止按钮事件
	     */
	    stopButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// 停止按钮置灰
				// stopButton.setEnabled(false);
				// 启动按钮可用
				// startButton.setEnabled(true);
				// 停止线程
				// taskSwitch.Off();
				 System.exit(0);
			}
		});
	}
	
	/**
	 * 入口函数
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			// 设置主题
			UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
			
			SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    // Start the application (启动应用)
                    LaunchWin app = new LaunchWin();
                    // 设置标题
                    app.setTitle("客户端测试程序");
                    // 宽:640, 高:480
                    app.setSize(640, 480); 
                    // 设置不能调整大小
                    app.setResizable(false); 
                    // 设置窗口相对指定组件的位置
                    app.setLocationRelativeTo(null);
                    // 设置窗体可见
                    app.setVisible(true);
                    
                    // 窗体退出事件
                    app.addWindowListener(new WindowAdapter() {
                    	
                    	 @Override
                         public void windowClosing(WindowEvent e) {
                             System.exit(0);
                         }
					});
                }
            });
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
